using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Command;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Query;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.Support;
using HCore.Tests.Runtime.ShootingEditor2D.Scripts.System.GunSystem;

namespace HCore.Tests.Runtime.ShootingEditor2D.Scripts.ViewController.GamePlay
{
    public class Gun : HCoreCommandMonoBehaviour
    {
        private Bullet mBulletComponent;
        private GunInfo mCurrentGun;
        [Autowired] private IGunSystem mGunSystem;

        protected override void Awake()
        {
            base.Awake();
            mBulletComponent = Get("Bullet").GetComponent<Bullet>();
            mCurrentGun = mGunSystem.CurrentGun;
        }

        public void Shoot()
        {
            // 子弹足够 && 闲置状态
            if (mCurrentGun.BulletCountInGun.Value > 0
                && mCurrentGun.GunState.Value == GunState.Idle)
            {
                // 创建子弹，并发射（动画）
                CreateBulletAndShoot();
                // 发送“发射子弹”命令
                SendCommand(ShootCommand.Instance);
            }
        }

        /// <summary>
        ///     装弹
        /// </summary>
        public void Reload()
        {
            var maxBulletCount = SendQuery(new MaxBulletCountQuery());
            // 必须是闲置状态
            if (mCurrentGun.GunState.Value == GunState.Idle
                // 弹夹不能是满的
                && mCurrentGun.BulletCountInGun.Value != maxBulletCount
                // 必须有后备子弹
                && mCurrentGun.BulletCountOutGun.Value > 0) SendCommand(ReloadCommand.Instance);
        }

        private void CreateBulletAndShoot()
        {
            var bulletTransform = mBulletComponent.transform;
            var bullet = Instantiate(mBulletComponent, bulletTransform.position, bulletTransform.rotation);
            var newBulletTransform1 = bullet.transform;
            // newBulletTransform1.SetParent(gameObject.transform);
            newBulletTransform1.localScale = bulletTransform.lossyScale;
            bullet.gameObject.SetActive(true);
        }
    }
}